﻿using CodeNotes.BLL;
using CodeNotes.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace MvcCodeNotes.Controllers
{
    public class TodoController : Controller
    {
        //
        // GET: /Todo/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Jsonp()
        {
            return View();
        }

        public ActionResult MenuEdit()
        {
            return View();
        }


        public string GetInformation(string callback, string LoginName, string Password)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            List<CW_User> user = new CW_UserLogic().GetModelList("");

            string result = String.Format("{0}({1})", callback, Serializer.Serialize(user));

            return result;
            
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Route()
        {
            return View();
        }

        //登录后，服务器上的数据更新本地localStorage
        [HttpPost]
        public JsonResult Login(string LoginName, string Password, FormCollection collection)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            try
            {
                CW_User user = new CW_UserLogic().GetModelByLoginnamePassword(LoginName, Password);
                if (user != null)
                {
                    dic.Add("state", "success");
                    dic.Add("uid", user.Id);
                }
                else
                {
                    dic.Add("state", "error");
                    dic.Add("msg", "用户名或密码不对");
                }
            }
            catch (Exception e)
            {
                dic.Add("state", "error");
                dic.Add("msg", e.Message.ToString());
            }

            return Json(dic, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Regist(string LoginName, string Password, FormCollection collection)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            try
            {
                //判断用户名是否存在
                int num = new CW_UserLogic().GetRecordCount("LoginName='" + LoginName + "'");

                if (num == 0)
                {
                    CW_User user = new CW_User();
                    user.LoginName = LoginName;
                    user.Password = Password;

                    int uid = new CW_UserLogic().Add(user);
                    dic.Add("state", "success");
                    dic.Add("uid", uid);
                }
                else
                {
                    dic.Add("state", "error");
                    dic.Add("msg", "此登录名已存在！");
                }
            }
            catch (Exception e)
            {
                dic.Add("state", "error");
                dic.Add("msg", e.Message.ToString());
            }
            return Json(dic);
        }

        [HttpPost]
        public JsonResult Save(FormCollection collection)
        {
            try
            {
                int uid = Convert.ToInt32(Request.Params["uid"]);
                string todolist = Request.Params["todolist"];
                //存在就更新，不存在则新增  
                CW_Todo oldModel = new CW_TodoLogic().GetModelByUserId(uid);
                if (oldModel != null && oldModel.Id > 0)
                {
                    oldModel.JsonContent = todolist;
                    oldModel.UpdateDate = DateTime.Now;
                    new CW_TodoLogic().Update(oldModel);
                }
                else
                {
                    CW_Todo todo = new CW_Todo();
                    todo.JsonContent = todolist;
                    todo.UserId = uid;
                    todo.CreateDate = DateTime.Now;
                    todo.UpdateDate = DateTime.Now;
                    new CW_TodoLogic().Add(todo);
                }
            }
            catch (Exception e)
            {
            }

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("state", "success");
            return Json(dic);
        }

        [HttpPost]
        public JsonResult GetlistByUid(FormCollection collection)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                int uid = Convert.ToInt32(Request.Params["uid"]);
                if (uid > 0)
                {
                    CW_Todo oldModel = new CW_TodoLogic().GetModelByUserId(uid);
                    if (oldModel != null && oldModel.Id > 0)
                    {
                        dic.Add("state", "success");
                        dic.Add("todolist", oldModel.JsonContent);
                    }
                    else
                    {
                        dic.Add("state", "error");
                        dic.Add("msg", "不存在todolist");
                    }
                }
                else
                {
                    dic.Add("state", "error");
                    dic.Add("msg", "用户id错误");
                }

            }
            catch (Exception e)
            {
                dic.Add("state", "error");
                dic.Add("msg", e.Message.ToString());
            }

            return Json(dic);
        }
    }
}
